package com.ufenlei.LTS {
	import flash.display.DisplayObject;

	/**
	 * @author bbn
	 */
	public class SimpleLayout {
		private var width:Number;
		private var height:Number;
		private var sList:Array = new Array();
		public function update(swidth:Number,sheight:Number):void{
			width = Math.max(swidth,0);
			height = Math.max(sheight,0);
		}
		public function freshView():void{
			sList.forEach(function(e:Object,idx:int,l:Array):void{
				trace('freshView',idx, ' ' ,e.disObj,e.top,e.left,e.bottom,e.right);
				var s:DisplayObject = (e.disObj as DisplayObject);
				updatePosition(s,(e.top as String),
								(e.left as String),
								parseFloat(e.bottom),
								parseFloat(e.right));
			});
		}
		private function updatePosition(s:DisplayObject,top:String = null,left:String = null,bottom:Number = NaN, right:Number = NaN):void{
			var pecent:Number;
			if(top!== null && top != ''){
				if( top.indexOf('%') >= 0){
					pecent = parseFloat(top)/100;
					s.y = (height - s.height)*pecent;
				}else{
					s.y = parseFloat(top);
				}
			}
			if(left!== null && left != ''){
				if( left.indexOf('%') > 0){
					pecent = parseFloat(left)/100;
					s.x = (width - s.width)*pecent;
				}else{
					s.x = parseFloat(left);
				}
			}
			if(!isNaN(bottom) ){
				s.y = height-bottom-s.height;
			}
			if(!isNaN(right) ){
				s.x = width - right - s.width;
			}
		}
		// 可以用 100，100%，center设置
		public function add(s:DisplayObject,top:String= null,left:String = null, bottom:Number = NaN, right:Number = NaN):void{
			var x:Object = {'disObj':s,
							'left':parse(left),
							'top':parse(top),
							'right':right,
							'bottom':bottom};
			sList.push(x);
		}
		private function parse(s:String):String{
			if(s == null) return null;
			if(s == 'center'){
				s = '50%';
			}
			var i:Number;
			if(s.indexOf('%') > -1){
				i = parseFloat(s);
				i = Math.max(0,Math.min(100,i));
				s = i+'%';
			}else{
				i = parseFloat(s);
				s = i+'';
			}
			return s;
		}
	}
}
